package com.skplanet.oneplusonewindow.moimserver.domain;

import java.io.Serializable;
import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
import javax.persistence.Table;

import org.hibernate.annotations.Type;

import com.fasterxml.jackson.annotation.JsonIgnore;

@Entity
@Table(name = "MOIM_PLAZABOARD")
public class Moim_PlazaBoard  implements Serializable{

	private static final long serialVersionUID = 1L;

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	private Long id;
	
	@Column(name = "title")
	private String title;
	
	@Column(name = "content")
	@Type(type="text")
	private String content;

	@Column(name = "readcount")
	private int readcount;
	
	@Column(name = "regdate")
	private String regdate;
	
	@Column(name = "realFilePath1")
	private String realFilePath1;
	
	@Column(name = "urlFilePath1")
	private String urlFilePath1;
	
	@Column(name = "userId")
	private long userId;

	@OneToMany(mappedBy = "plazaboard", fetch = FetchType.LAZY, cascade = {CascadeType.ALL})
	@OrderBy("regdate ASC" )
	private List<Moim_PlazaComment> comments;
	
	@ManyToOne(fetch = FetchType.EAGER)
	@JoinColumn(name = "plaza_id")
	@JsonIgnore
	private Moim_Plaza plaza;
	
	
	
	public Moim_PlazaBoard(){
		setReadcount(0);
		Date date = new Date();
		DateFormat format1 = DateFormat.getDateInstance(DateFormat.MEDIUM);
		setRegdate(format1.format(date));
	}

	public long getUserId() {
		return userId;
	}

	public void setUserId(long userId) {
		this.userId = userId;
	}

	public Moim_Plaza getPlaza() {
		return plaza;
	}

	public void setPlaza(Moim_Plaza plaza) {
		this.plaza = plaza;
	}
	
	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getContent() {
		return content.replaceAll("\n", "<br>");
	}

	public void setContent(String content) {
		this.content = content;
	}



	public int getReadcount() {
		return readcount;
	}

	public void setReadcount(int readcount) {
		this.readcount = readcount;
	}

	public String getRegdate() {
		return regdate;
	}

	public void setRegdate(String regdate) {
		this.regdate = regdate;
	}
	
	

	public List<Moim_PlazaComment> getComments() {
		return comments;
	}

	public void setComments(List<Moim_PlazaComment> comments) {
		this.comments = comments;
	}
	
	public void addComment(Moim_PlazaComment comment){
		if(comments == null){
			comments = new ArrayList<Moim_PlazaComment>();
		}
		comments.add(comment);
	}

	public String getRealFilePath1() {
		return realFilePath1;
	}

	public void setRealFilePath1(String realFilePath1) {
		this.realFilePath1 = realFilePath1;
	}

	public String getUrlFilePath1() {
		return urlFilePath1;
	}

	public void setUrlFilePath1(String urlFilePath1) {
		this.urlFilePath1 = urlFilePath1;
	}
	
}
